package ar.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ar.activity.R.string;
import ar.globals.Globals;

public class ShareGetContactsActitvity extends BaseActivity {

	public static final int PICK_CONTACT = 1;
	private EditText etContacts;
	private Button bPickContacts;
	private Button bOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.share_get_contacts);
		super.onCreate(savedInstanceState);
		tvWhatScreen.setText("Get Contacts");

		if (!Globals.shareCode.trim().equals("")) {
			AlertDialog.Builder infoDlg = new Builder(this);
			infoDlg.setTitle(res.getString(string.shareImportantNote));
			infoDlg.setMessage(res.getString(string.shareAlarmCode)
					+ Globals.shareCode.trim());

			infoDlg.setPositiveButton(res.getString(string.OK),
					new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
			infoDlg.show();
			((EditText) findViewById(R.id.etShareCode))
					.setText(Globals.shareCode);
		}		

		etContacts = (EditText) findViewById(R.id.etShareContacts);
		bOk = (Button) findViewById(R.id.bShareOk);
		bOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String[] numbers = etContacts.getText().toString().split("\n");
				Intent i = new Intent();
				i.putExtra("numberArray", numbers);
				setResult(RESULT_OK, i);

				String newCode = ((EditText) findViewById(R.id.etShareCode))
						.getText().toString().trim();
				if (newCode.trim().equals("")) {
					Globals.isNeedSendMessage = false;
				} else {
					if (newCode.equals(Globals.shareCode)) {
						Globals.isNeedSendMessage = false;
					} else {
						Globals.shareCode = newCode;
						Globals.isNeedSendMessage = true;
					}
				}
				finish();
			}
		});

		bPickContacts = (Button) findViewById(R.id.bSharePickContact);
		bPickContacts.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK,
						ContactsContract.Contacts.CONTENT_URI);
				startActivityForResult(intent, PICK_CONTACT);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {
					getContactInfo(data);
				}
			}
			break;
		}
	}

	protected void getContactInfo(Intent intent) {
		Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
		String name = "";
		String phoneNumber = "";
		while (cursor.moveToNext()) {
			String contactId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));
			name = cursor
					.getString(cursor
							.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

			String hasPhone = cursor
					.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

			if (hasPhone.equalsIgnoreCase("1"))
				hasPhone = "true";
			else
				hasPhone = "false";
			if (Boolean.parseBoolean(hasPhone)) {
				Cursor phones = getContentResolver().query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID
								+ " = " + contactId, null, null);
				while (phones.moveToNext()) {
					phoneNumber = phones
							.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				}
				phones.close();
			}
			etContacts.append(phoneNumber.replace("-", "") + "\n");
			Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT)
					.show();
		}
		cursor.close();
	}
}
